package com.thinkTank.oa.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.thinkTank.oa.entity.Permission;
import com.thinkTank.oa.service.PermissionService;

/***
 * 访问资源类
 * @date 2018-08-09
 * @author dq
 *
 */
@RequestMapping("/permission")
@Controller
public class PermissionController  extends BaseController {
	
	private final Logger logger = LoggerFactory.getLogger(PermissionController.class);
	
	@Autowired
	private PermissionService permissionService;
	/***
	 * 显示添加资源页面
	 * @date 2018-08-15
	 * @author dq
	 * @return
	 */
	@ RequestMapping(value= "showAddPermission")
	public String showAddPermission(Model model){
		try{
			if(!model.containsAttribute("permission")){
				model.addAttribute("permission", new Permission());
			}
			List <Permission> menus = permissionService.findPermissionByMenu();
			model.addAttribute("menus",menus);
		}catch(Exception e){
			logger.error(e.getMessage());
		}
		
		return "showaddpermission";
		
	}
	/***
	 * 添加资源
	 * @date 2018-08-15
	 * @author dq
	 * @return
	 */
	@RequestMapping(value= "addPermission")
	public String addPermission( @Valid Permission permission , BindingResult result,Model model){
		try{
			Permission urlPer =permissionService.findPermissionByUrl(permission.getUrl());
			if(urlPer!=null){
				 result.rejectValue("url","urlFormat", "该资源已经存在");
			}
			if (result.hasErrors()) {
				/*List<ObjectError> list = result.getAllErrors();
				for (ObjectError error : list) {
					System.out.println(error.getDefaultMessage());
				}*/
				/*List <Permission> menus = permissionService.findPermissionByMenu();
				model.addAttribute("menus",menus);*/
				model.addAttribute("permission", permission);
				 return showAddPermission(model);
			}
			if(permission.getParentId()==-1){
				permission.setParentId(null);
			}
			permissionService.add(permission);
			
		}catch(Exception e){
			logger.error(e.getMessage());
		}
		return "redirect:/permission/showPermissionList";
	}
	/***
	 * 显示资源列表
	 * @author dq
	 * @date 2018-08-15
	 * @return
	 */
	@RequestMapping(value= "showPermissionList")
	public String showPermissionList(Integer pageNo,HttpServletRequest request,String searchKeyword){
		
		try{
			if(null==pageNo){
				pageNo=1;
			}
			 
			 PageHelper.startPage(pageNo, DEFAULT_COUNT);
			 Permission permission = new  Permission();
			 if(searchKeyword!=null&&!"".endsWith(searchKeyword)){
				 if(searchKeyword.indexOf("/")>=0){
					 permission.setUrl(searchKeyword);;
				 }else{
					 permission.setName(searchKeyword);
				 }
			 }
			 List<Permission> list = permissionService.queryAll(permission);
			 PageInfo<Permission> pageInfo = new PageInfo<Permission>(list);
			 request.setAttribute("pageInfo", pageInfo);
		}catch(Exception e){
			logger.error(e.getMessage());
		}
		return "showpermissionlist";
	}
	/***
	 * 显示修改资源页面
	 * @date 2018-08-15
	 * @author dq
	 * @return
	 */
	@RequestMapping(value= "showUpdatePermission")
	public String showUpdatePermission(Integer permissionId,Model model){
		try{
			List <Permission> menus = permissionService.findPermissionByMenu();
			model.addAttribute("menus",menus);
			Permission per = permissionService.getById(permissionId);
			if(!model.containsAttribute("permission")){
				model.addAttribute("permission", per);
			}
		
			
		}catch(Exception e){
			logger.error(e.getMessage());
		}
		
		return "showupdatePermission";
	}
	/***
	 * 修改资源方法
	 * @date 2018-08-15
	 * @author dq
	 * @return
	 */
	@RequestMapping(value= "updatePermission")
	public String updatePermission(@Valid  Permission permission,BindingResult result,Model model){
		try{
			Permission per = permissionService.getById(permission.getPid());
			if(!per.getUrl().equals(permission.getUrl())){//当他修改url的时候在判断url存不存在
				Permission urlPer =permissionService.findPermissionByUrl(permission.getUrl());
				if(urlPer!=null){
					 result.rejectValue("url","urlFormat", "该资源已经存在");
				}
			}
			
			if (result.hasErrors()) {
				 model.addAttribute("permission", permission);
				 return showAddPermission(model);
			}
			
			if(null != per){
				if(permission.getParentId()!=-1){
					per.setParentId(permission.getParentId());
				}
				if(permission.getName()!=null&&!"".equals(permission.getName())){
					per.setName(permission.getName());
					
				}
				if(permission.getUrl()!=null&&!"".equals(permission.getUrl())){
					per.setUrl(permission.getUrl());
					
				}
				if(permission.getDescription()!=null&&!"".equals(permission.getDescription())){
					
					per.setDescription(permission.getDescription());
				}
			}	
			permissionService.update(per);
		}catch(Exception e){
			logger.error(e.getMessage());
		}
		
		return "redirect:/permission/showPermissionList";
	}
}
